<?php

namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;

class RefreshMaterialDetail extends Command
{
    protected function configure()
    {
        $this->setName('refresh_material_detail')
            ->setDescription('刷新数据');
    }

    protected function execute(Input $input, Output $output)
    {
        $end = date('Y-m-d', time());
        $start = date("Y-m-d",strtotime("-7 days",strtotime($end)));

        $detail = db('gzh_material_detail')
            ->where('refresh', 1)
            ->whereBetween('extension_date', [$start, $end])
            ->select();

        $params['key'] = "TpzXabIqd8u^1crS";
        $url = 'https://bowen.qingguo959.com/api/tuiguang/getinfo';
        foreach ($detail as $k => $v) {
            $params['time'] = $v['extension_date'];
            $count = 0;
            $one_proportion = 0;
            foreach (explode(',', $v['extension_id']) as $extensionId) {
                $params['tid'] = $extensionId;
                // 获取公众号对应的推广总收入
                $extensionInfo = $this->curlRequest($url, $params);
                if ($extensionInfo['code'] == 1) {
                    $isHave = db('gzh_extension')
                        ->where('detail_id', $v['id'])
                        ->where('extension_id', $extensionId)
                        ->find();
                    if ($isHave) {
                        db('gzh_extension')
                            ->where('extension_id', $extensionId)->where('detail_id', $v['id'])
                            ->update([
                                'extension_date' => $v['extension_date'],
                                'income' => $extensionInfo['data']['income'],
                                'income_one' => $extensionInfo['data']['income_one'],
                                'income_two' => $extensionInfo['data']['income_two'],
                                'income_three' => $extensionInfo['data']['income_three'],
                                'update_time' => time()
                            ]);
                    } else {
                        db('gzh_extension')->insert([
                            'extension_id' => $extensionId,
                            'income' => $extensionInfo['data']['income'],
                            'income_one' => $extensionInfo['data']['income_one'],
                            'income_two' => $extensionInfo['data']['income_two'],
                            'income_three' => $extensionInfo['data']['income_three'],
                            'extension_date' => $v['extension_date'],
                            'detail_id' => $v['id'],
                            'create_time' => time()
                        ]);
                    }
                    $count += $extensionInfo['data']['income'];
                    $one_proportion += $extensionInfo['data']['income_one'];
                } else {
                    echo $extensionId . $extensionInfo['msg'];
                }
                $effect = 0;
                $proportion = $count == 0 ? 0 : $count / $v['consume'] * 100;
                $oneProportion = $one_proportion == 0 ? 0 : $one_proportion / $v['consume'] * 100;
                $book = db('ads_material_book')->where('id', $v['book_id'])->find();
                if ($book) {
                    $book = json_decode($book['roi'], true);
                    if (isset($book['百度'])) {
                        $roi = $book['百度']['roi'];
                        $roiCount = $book['百度']['roi_count'];
                    } else {
                        $roi = 0;
                        $roiCount = 0;
                    }
                    $effect = $this->getEffect($roi, $roiCount, $oneProportion, $proportion, $v['consume']);
                }
                db('gzh_material_detail')->where('id', $v['id'])->update(['effect'=>$effect]);
            }

        }
        echo '执行完毕';
    }

    /**
     * Notes: 获取有效标识
     * @param $roi 单日roi
     * @param $roiCount 总roi
     * @param $oneProportion 单日回收
     * @param $proportion 总回收
     * @param $consume 总消耗
     * @return int|string
     * @author: Zhu
     * @since: 2022/12/9 17:17
     */
    private function getEffect($roi, $roiCount, $oneProportion, $proportion, $consume)
    {
        // 浮点转整形
        $roi = $roi * 10000;
        $roiCount = $roiCount * 10000;
        $oneProportion = $oneProportion * 10000;
        $proportion = $proportion * 10000;
        $effect = 3;
        if (($roi * 0.35 > $oneProportion || $proportion < $roiCount) && $consume >= 600) {
            $effect = '1';
        }
        if ((($roi * 0.7 > $oneProportion && $roi * 0.35 <= $oneProportion) || ($proportion < $roiCount)) && ($consume >= 400 && $consume < 600)) {
            $effect = '2';
        }
        if ($roi * 0.7 <= $oneProportion && $proportion < $roiCount) {
            $effect = '3';
        }
        if ($roi <= $oneProportion || $proportion >= $roiCount) {
            $effect = '4';
        }
        if ($roi * 2 <= $oneProportion || ($roiCount * 2 <= $proportion)) {
            $effect = '5';
        }
        return $effect;
    }

    /**
     * Notes: 请求外部接口
     * @param $url
     * @param null $data
     * @return mixed
     * @since: 2022/12/9 17:40
     * @author: Zhu
     */
    private function curlRequest($url, $data = null)
    {
        $postUrl = $url;
        $curl = curl_init();//初始化curl
        curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);//
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);//参数
        $content = curl_exec($curl);
        curl_close($curl);
        return json_decode($content, true);
    }

}